package com.letoken.platform.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.pub.annotation.auth.Permission;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.repo.admin.req.user.FundCirculationRecordReq;
import com.letoken.platform.repo.admin.req.user.FundRankingListBonusRecordReq;
import com.letoken.platform.repo.admin.req.user.FundRecordReq;
import com.letoken.platform.repo.app.dao.*;
import com.letoken.platform.repo.app.po.*;
import com.letoken.platform.repo.utlis.PageDataUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/fund")
public class FundController {


    @Resource
    private AppFundV2InfoMapper appFundV2InfoMapper;

    @Resource
    private AppFundV2DetailMapper appFundV2DetailMapper;

    @Resource
    private AppUserAssetsHistoryMapper appUserAssetsHistoryMapper;

    @Resource
    private AppRankingDetailMapper appRankingDetailMapper;

    @Resource
    private AdminFundV2ConfigMapper adminFundV2ConfigMapper;


    @Permission(uri = "/fund/issuance/record/page", qx = "/fund/issuance/record/page")
    @PostMapping("/issuance/record/page")
    public ApiResponse<Page<AppFundV2Info>> recordPage(@RequestBody @Validated FundRecordReq req) {
        Page<AppFundV2Info> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<AppFundV2Info> appFundInfoList = appFundV2InfoMapper.page(page, req);
        return ApiResponse.create(PageDataUtil.getPageData(appFundInfoList));
    }

    @Permission(uri = "/fund/issuance/summarize", qx = "/fund/issuance/summarize")
    @PostMapping("/issuance/summarize")
    public ApiResponse<Page<AppFundInfo>> summarize() {

        Map<String, BigDecimal> summarizeMap = new HashMap<>();
        summarizeMap.put("totalIssueFund", appFundV2InfoMapper.sumFund());
        summarizeMap.put("totalFundRetained", appFundV2InfoMapper.sumRetained());
        summarizeMap.put("yesterdayIssueFund", appFundV2DetailMapper.sumAmountByDateAndType(LocalDate.now().minusDays(1), 1));
        summarizeMap.put("todayIssueFund", appFundV2DetailMapper.sumAmountByDateAndType(LocalDate.now(), 1));

        LocalDate now = LocalDate.now();
        LocalDate firstDayOfMonth = now.withDayOfMonth(1);
        LocalDate lastDayOfMonth = now.with(TemporalAdjusters.lastDayOfMonth());
        summarizeMap.put("currentMonthIssueFund", appFundV2DetailMapper.sumAmountByCreateTimeAndType(LocalDateTime.of(firstDayOfMonth, LocalTime.MIN), LocalDateTime.of(lastDayOfMonth, LocalTime.MAX), 1));
        return ApiResponse.create(summarizeMap);
    }


    @Permission(uri = "/fund/circulate/record/page", qx = "/fund/circulate/record/page")
    @PostMapping("/circulate/record/page")
    public ApiResponse<Page<AppFundV2Detail>> circulateRecordPage(@RequestBody @Validated FundCirculationRecordReq req) {
        Page<AppFundV2Info> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<AppFundV2Info> appFundInfoList = appFundV2DetailMapper.page(page, req);
        return ApiResponse.create(PageDataUtil.getPageData(appFundInfoList));
    }

    @Permission(uri = "/fund/circulate/summarize", qx = "/fund/circulate/summarize")
    @PostMapping("/circulate/summarize")
    public ApiResponse<?> circulateSummarize() {
        Map<String, BigDecimal> circulateSummarizeMap = new HashMap<>();
        circulateSummarizeMap.put("todayCirculate", appFundV2DetailMapper.sumAmountByDate(LocalDate.now()));
        circulateSummarizeMap.put("yesterdayCirculate", appFundV2DetailMapper.sumAmountByDate(LocalDate.now().minusDays(1)));

        LocalDate now = LocalDate.now();
        LocalDate firstDayOfMonth = now.withDayOfMonth(1);
        LocalDate lastDayOfMonth = now.with(TemporalAdjusters.lastDayOfMonth());
        circulateSummarizeMap.put("currentMonthCirculate", appFundV2DetailMapper.sumAmountByCreateTime(LocalDateTime.of(firstDayOfMonth, LocalTime.MIN), LocalDateTime.of(lastDayOfMonth, LocalTime.MAX)));

        circulateSummarizeMap.put("totalCirculate", appFundV2DetailMapper.sumAmount());
        circulateSummarizeMap.put("totalRetained", appFundV2InfoMapper.sumRetained());
        return ApiResponse.create(circulateSummarizeMap);
    }

    @Permission(uri = "/fund/ranking_list/bonus/page", qx = "/fund/ranking_list/bonus/page")
    @PostMapping("/ranking_list/bonus/page")
    public ApiResponse<Page<AppRankingDetail>> rankingListBonusPage(@RequestBody @Validated FundRankingListBonusRecordReq req) {
        Page<AppRankingDetail> page = new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize());
        Page<AppRankingDetail> appFundInfoList = appRankingDetailMapper.page(page, req);
        return ApiResponse.create(PageDataUtil.getPageData(appFundInfoList));
    }

    @Permission(uri = "/fund/ranking_list/bonus/summarize", qx = "/fund/ranking_list/bonus/summarize")
    @PostMapping("/ranking_list/bonus/summarize")
    public ApiResponse<?> rankingListBonusSummarize() {
        Map<String, BigDecimal> circulateSummarizeMap = new HashMap<>();
        circulateSummarizeMap.put("yesterdayDailyRankingListBonus", appUserAssetsHistoryMapper.sumAmountByDateAndType(LocalDate.now().minusDays(1), 25));
        circulateSummarizeMap.put("lastWeekWeeklyRankingListBonus", appUserAssetsHistoryMapper.sumAmountByDateTimeAndType(LocalDateTime.of(LocalDate.now().minusDays(7), LocalTime.MIN), LocalDateTime.now(), 26));
        circulateSummarizeMap.put("totalDailyRankingListBonus", appUserAssetsHistoryMapper.sumAmountByType(25));
        circulateSummarizeMap.put("totalWeeklyRankingListBonus", appUserAssetsHistoryMapper.sumAmountByType(26));
        return ApiResponse.create(circulateSummarizeMap);
    }

    @Permission(uri = "/fund/config/list", qx = "/fund/config/list")
    @PostMapping("/config/list")
    public ApiResponse<?> configList() {

        List<AdminFundV2Config> configs = adminFundV2ConfigMapper.listAsc();
        for (int i = 0; i < configs.size(); i++) {
            AdminFundV2Config config = configs.get(i);
            int index = i + 1;
            if (index < configs.size()) {
                AdminFundV2Config nextConfig = configs.get(index);
                config.setMaxNumber(nextConfig.getMinNumber());
            }
        }
        return ApiResponse.create(configs);
    }

    @Permission(uri = "/fund/config/add", qx = "/fund/config/add")
    @PostMapping("/config/add")
    public ApiResponse<?> configAdd(@RequestBody List<AdminFundV2Config> configs) {
        if (null != configs && !configs.isEmpty()) {
            for (AdminFundV2Config config : configs) {
                config.setCreateTime(LocalDateTime.now());
                adminFundV2ConfigMapper.insert(config);
            }
        }
        return ApiResponse.create();
    }

    @Permission(uri = "/fund/config/update", qx = "/fund/config/update")
    @PostMapping("/config/update")
    public ApiResponse<?> configUpdate(@RequestBody List<AdminFundV2Config> configs) {
        if (null != configs && !configs.isEmpty()) {
            for (AdminFundV2Config config : configs) {
                if(null != config.getId()){
                    adminFundV2ConfigMapper.updateById(config);
                }
            }
        }
        return ApiResponse.create();
    }
}
